package com.xwq.smartgelleryhub.model.enums;

import com.xwq.smartgelleryhub.exception.BusinessException;
import com.xwq.smartgelleryhub.exception.ErrorCode;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;

@Getter
@AllArgsConstructor
public enum FileUploadBizEnum {
    /** 用户头像上传 */
    USER_AVATAR("avatar", Arrays.asList("jpg", "jpeg", "png"), 2 * 1024 * 1024L),
    /** 文章封面图上传 */
    ARTICLE_COVER("article", Arrays.asList("jpg", "jpeg", "png", "webp"), 5 * 1024 * 1024L),
    /** 其他文件上传 */
    DOCUMENT("document", Arrays.asList("pdf", "doc", "docx", "txt"), 10 * 1024 * 1024L),
    /** 图片文件上传 */
    IMAGE("image", Arrays.asList("jpg", "jpeg", "png", "webp"), 5 * 1024 * 1024L);

    /** 上传路径前缀 */
    private final String uploadPathPrefix;
    /** 允许的文件格式 */
    private final List<String> allowFormats;
    /** 最大文件大小 (单位: 字节) */
    private final long maxSize;

    /**
     * 根据业务类型获取枚举实例
     */
    public static FileUploadBizEnum getByPrefix(String prefix) {
        return Arrays.stream(values())
                .filter(e -> e.uploadPathPrefix.equals(prefix))
                .findFirst()
                .orElseThrow(() -> new BusinessException(ErrorCode.PARAMS_ERROR, "无效的上传业务类型"));
    }
}